const db = require('../config/db.config');

class Word {
    // 创建新的单词游戏会话
    static async createSession(userId, sessionName) {
        const [result] = await db.execute(
            'INSERT INTO word_game_sessions (user_id, session_name) VALUES (?, ?)',
            [userId, sessionName]
        );
        return result.insertId;
    }

    // 在会话中保存生成的单词
    static async saveWords(sessionId, userId, level, content) {
        const [result] = await db.execute(
            'INSERT INTO word_game_messages (session_id, user_id, level, content) VALUES (?, ?, ?, ?)',
            [sessionId, userId, level, content]
        );
        return result.insertId;
    }

    // 获取用户的所有会话
    static async getSessions(userId) {
        const [rows] = await db.execute(
            'SELECT * FROM word_game_sessions WHERE user_id = ? AND is_deleted = 0 ORDER BY created_at DESC',
            [userId]
        );
        return rows;
    }

    // 获取单个会话信息
    static async getSessionById(sessionId, userId) {
        const [rows] = await db.execute(
            'SELECT * FROM word_game_sessions WHERE session_id = ? AND user_id = ? AND is_deleted = 0',
            [sessionId, userId]
        );
        return rows[0];
    }

    // 获取会话中的所有单词记录
    static async getSessionWords(sessionId, userId) {
        const [rows] = await db.execute(
            'SELECT * FROM word_game_messages WHERE session_id = ? AND user_id = ? ORDER BY created_at DESC',
            [sessionId, userId]
        );
        return rows;
    }

    // 更新通关状态
    static async updateCompletionStatus(messageId, userId, isCompleted) {
        const [result] = await db.execute(
            'UPDATE word_game_messages SET is_completed = ? WHERE message_id = ? AND user_id = ?',
            [isCompleted ? 1 : 0, messageId, userId]
        );
        return result.affectedRows > 0;
    }

    // 软删除会话
    static async deleteSession(sessionId, userId) {
        const [result] = await db.execute(
            'UPDATE word_game_sessions SET is_deleted = 1 WHERE session_id = ? AND user_id = ?',
            [sessionId, userId]
        );
        return result.affectedRows > 0;
    }

    // 删除单词消息
    static async deleteWordMessage(messageId, userId) {
        const [result] = await db.execute(
            'DELETE FROM word_game_messages WHERE message_id = ? AND user_id = ?',
            [messageId, userId]
        );
        return result.affectedRows > 0;
    }

    // 更新会话名称
    static async updateSessionName(sessionId, userId, sessionName) {
        const [result] = await db.execute(
            'UPDATE word_game_sessions SET session_name = ? WHERE session_id = ? AND user_id = ?',
            [sessionName, sessionId, userId]
        );
        return result.affectedRows > 0;
    }

    // 添加收藏
    static async addFavorite(userId, wordContent) {
        const [result] = await db.execute(
            'INSERT INTO word_favorites (user_id, word_content) VALUES (?, ?)',
            [userId, wordContent]
        );
        return result.insertId;
    }

    // 取消收藏（软删除）
    static async removeFavorite(favoriteId, userId) {
        const [result] = await db.execute(
            'UPDATE word_favorites SET is_deleted = 1 WHERE favorite_id = ? AND user_id = ? AND is_deleted = 0',
            [favoriteId, userId]
        );
        return result.affectedRows > 0;
    }

    // 获取收藏列表
    static async getFavorites(userId) {
        const [rows] = await db.execute(
            'SELECT * FROM word_favorites WHERE user_id = ? AND is_deleted = 0 ORDER BY created_at DESC',
            [userId]
        );
        return rows;
    }
}

module.exports = Word; 